﻿#region Styx Namespaces

using System;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
#endregion
#region System Namespaces
using System.Linq;
using System.Collections.Generic;
#endregion

namespace GiwinMonk
{

    /// <summary>
    /// Format = [Main Spell] | [Higher Priority Spells] | [Finisher Spells]
    /// </summary>

    public partial class Monk : CombatRoutine
    {
        public void GeneralRotation()
        {
            if (StyxWoW.Me.CurrentTarget != null)
                using (StyxWoW.Memory.AcquireFrame())
                {
                    //if (MIRsettings.Instance.Trinket1)

                    #region Special Attacks (Highest Priority)

                    /*
                    if (DownTotem() && SpinningFireBlossom())
                    {.
                        Logging.Write("Totems Detected");
                        foreach (var item in ObjectManager.GetObjectsOfType<WoWItem>(false, false).Where(o => o.IsTotem))
                        {
                            switch (item.Name)
                            {
                                case "Fire Elemental Totem":
                                    break;
                                case "Grounding Totem":
                                    break;
                                case "Earthbind Totem":
                                    break;
                                case "Healing Stream Totem":
                                    break;
                                case "Capacitor Totem":
                                    break;
                                case "Stormlash Totem":
                                    break;
                                case "Earth Elemental Totem":
                                    break;
                            }
                        }
                    }
                    */
                    if ((StyxWoW.Me.IsSafelyFacing(StyxWoW.Me.CurrentTarget) &&
                        StyxWoW.Me.CurrentTarget != null) && (
                        !StyxWoW.Me.IsCasting &&
                       !StyxWoW.Me.IsChanneling))
                    {
                        /*
                        if (StyxWoW.Me.CurrentTarget != null &&
                            StyxWoW.Me.CurrentTarget.HasAura("Immunity"))
                        {
                            if (StyxWoW.Me.PartyMembers[0].CurrentTarget != null)
                            {
                                StyxWoW.Me.PartyMembers[0].CurrentTarget.Target();
                            }
                            else if (StyxWoW.Me.RaidMembers[0].CurrentTarget != null)
                            {
                                StyxWoW.Me.RaidMembers[0].CurrentTarget.Target();
                            }
                        }

                        */
                        if (Adds.Count > 0 && Helpers.Unit.TargetsWithinMeleeDistanceAndMeFacingTarget()
                            .GetEnumerator().MoveNext() &&
                            (FistsofFury() || LegSweep()))
                        {
                            if (FistsofFury() || FistsofFuryEmptyChi())
                            {
                                Logging.Write("[Special Attack]: Fists of Fury");
                                if (StyxWoW.Me.CurrentChi < 3 && ChiBrew())
                                {
                                    Logging.Write("[Special]: Chi Brew for Fists of Fury");
                                    InstantCast("Chi Brew");
                                }
                                else
                                {
                                    WoWMovement.MoveStop();
                                    LongCast("Fists Of Fury");
                                    WoWMovement.MoveStop();
                                }
                            }
                            else if (LegSweep())
                            {
                                Logging.Write("[Special Attack]: Leg Sweep");
                                InstantCast("Leg Sweep");
                            }
                        }
                        if (Helpers.Unit.SwitchToTargetLowHealth().GetEnumerator().MoveNext())
                        {
                            if ((StyxWoW.Me.PartyMembers[0].CurrentTarget != null ||
                                 StyxWoW.Me.RaidMembers[0].CurrentTarget != null))
                            {
                                foreach (WoWPlayer unit in Helpers.Unit.SwitchToTargetLowHealth().Where(unit => unit != null))
                                {
                                    Logging.Write("[Special]: Switching target from " + StyxWoW.Me.CurrentTarget.Name + " to " + unit.Name);
                                    unit.Target();
                                }
                            }
                        }
                        if (
                            (Adds.Count > 0 && Helpers.Unit.OtherRangedTargetsTargettingMeAndCastingHostileSpell().GetEnumerator().
                                 MoveNext() ||
                             Adds.Count > 0 && StyxWoW.Me.HasAura("Frost Nova")) && ZenMeditation())
                        {
                            Logging.Write("[Special Attack]: Zen Meditation");
                            WoWMovement.MoveStop();
                            LongCast("Zen Meditation");
                            WoWMovement.MoveStop();
                        }

                        #endregion

                        if (isCrowdControlled())
                        {
                            Logging.Write("Using Trinket on CrowdControl Spell");
                            StyxWoW.Me.Inventory.Equipped.Trinket1.Use(); // sometimes doesnt work on 1 cast... 
                        }
                        // }

                        //Logging.Write(Helpers.Unit.TargetsWithinMeleeDistanceAndMeFacingTarget().GetEnumerator().MoveNext().ToString() + ": TargetsWithinMeleeDisanceAndMeFacingTarget");
                        //Logging.Write(Helpers.Unit.OtherRangedTargetsTargettingMeAndCastingHostileSpell().GetEnumerator().MoveNext().ToString() + ": OtherRangedTargetsTargettingMeAndCastingHostileSpell");
                        //Logging.Write(Helpers.Unit.SomeoneIsCastingCrowdControlSpellOnMe().GetEnumerator().MoveNext().ToString() + ": SomeoneIsCastingCrowdControlSpellOnMe");

                        #region Standard

                        if (MIRsettings.Instance.HealingSphere)
                        {
                            if (HealingSphere() &&
                                !Disable() &&
                                !DampenHarm() &&
                                !ExpelHarm() &&
                                !TouchofDeath() &&
                                !Roll())
                            {
                                Logging.Write("[Healing] Casting Healing Sphere on Myself");
                                SpellManager.Cast("Healing Sphere");
                                SpellManager.ClickRemoteLocation(StyxWoW.Me.Location);
                                SpellManager.StopCasting();
                            }
                        }
                        if (MIRsettings.Instance.Disable)
                        {
                            if (Disable() &&
                                StyxWoW.Me.CurrentTarget.IsPlayer &&
                                !InvokeXuen() &&
                                !HealingSphere() &&
                                !FistsofFury() &&
                                !TouchofDeath())
                            {
                                InstantCast("Disable");
                               
                            }
                        }
                        //if (MIRsettings.Instance.ChiWave)
                        //{
                            if (ChiWave() &&
                                !InvokeXuen() &&
                                !TouchofDeath())
                            {
                                InstantCast("Chi Wave");
                            }
                        //}
                        if (MIRsettings.Instance.FistsOfFury)
                        {
                            if (FistsofFury() &&
                                !InvokeXuen() &&
                                !TouchofDeath())
                            {
                                WoWMovement.MoveStop();
                                LongCast("Fists of Fury");
                                WoWMovement.MoveStop();
                            }
                        }
                        if (MIRsettings.Instance.Jab)
                        {
                            if (Jab() &&
                                !InvokeXuen() &&
                                !TouchofDeath() &&
                                !ExpelHarm() &&
                                !DampenHarm())
                            {
                                InstantCast("Jab");
                            }
                        }
                        if (MIRsettings.Instance.RisingSunKick)
                        {
                            if (RisingSunKick() &&
                                !TouchofDeath() &&
                                !TouchofKarma())
                            {
                                InstantCast("Rising Sun Kick");
                            }
                        }
                        if (MIRsettings.Instance.TigerPalm)
                        {
                            if (TigerPalm() &&
                                !BlackoutKick() &&
                                !TouchofDeath())
                            {
                                InstantCast("Tiger Palm");
                            }
                        }
                        if (MIRsettings.Instance.TouchOfKarma)
                        {
                            //if (!MIRsettings.Instance.GlyphOfTouchOfKarma)
                            {
                                if (TouchofKarma() &&
                                    !TouchofDeath())
                                {
                                    InstantCast("Touch of Karma");
                                }
                            }
                            //else if (MIRsettings.Instance.GlyphOfTouchOfKarma)
                            {
                                if (GlyphofTouchofKarma() &&
                                    !TouchofDeath())
                                {
                                    
                                }
                            }
                        }
                        if (Helpers.Unit.DetectTotem().GetEnumerator().MoveNext()
                            && SpinningFireBlossom())
                        {
                            foreach (WoWUnit unit in Helpers.Unit.DetectTotem())
                            {
                                if (unit != null)
                                {
                                    Logging.Write("[Special]: Kill Grounding Totem");
                                    unit.Target();
                                    InstantCast("Spinning Fire Blossom");
                                    StyxWoW.Me.TargetLastTarget();
                                }
                            }
                        }
                        if (MIRsettings.Instance.ExpelHarm)
                        {
                            if (ExpelHarm() &&
                                !TouchofKarma() &&
                                !TouchofDeath())
                            {
                                InstantCast("Expel Harm");
                            }
                        }
                        if (MIRsettings.Instance.BlackoutKick)
                        {
                            if (BlackoutKick() &&
                                !FistsofFury() &&
                                !TouchofDeath())
                            {
                                InstantCast("Blackout Kick");
                            }
                        }
                        if (MIRsettings.Instance.DampenHarm)
                        {
                            if (DampenHarm() &&
                                !TouchofKarma() &&
                                !TouchofDeath())
                            {
                                InstantCast("Dampen Harm");
                            }
                        }
                        if (MIRsettings.Instance.SpinningCraneKick)
                        {
                            if (SpinningCraneKick() &&
                                !RisingSunKick() &&
                                !BlackoutKick() &&
                                !Disable() &&
                                !TouchofDeath())
                            {
                                LongCast("Spinning Crane Kick");
                            }
                        }
                        if (MIRsettings.Instance.FlyingSerpentKick)
                        {
                            if (FlyingSerpentKick() &&
                                !TouchofDeath())
                            {
                                InstantCast("Flying Serpent Kick");
                            }
                        }
                        if (MIRsettings.Instance.Roll)
                        {
                            if (Roll() &&
                                !FlyingSerpentKick() &&
                                !TouchofDeath())
                            {
                                InstantCast("Roll");
                            }
                        }
                        if (MIRsettings.Instance.GrappleWeapon)
                        {
                            if (GrappleWeapon() &&
                                !isChiFull() &&
                                !TouchofDeath())
                            {
                                InstantCast("Grapple Weapon");
                            }
                        }

                        #endregion

                        #region Interrupt

                        if (MIRsettings.Instance.QuakingPalm)
                        {
                            if (QuakingPalm() && !FortifyingBrew())
                            {
                                InstantCast("Quaking Palm");
                            }
                        }
                        if (MIRsettings.Instance.Paralysis)
                        {
                            if (Paralysis() &&
                                !QuakingPalm())
                            {
                                InstantCast("Paralysis");
                            }
                        }
                        if (MIRsettings.Instance.SpearHandStrike)
                        {
                            if (SpearHandStrike() &&
                                !Paralysis())
                            {
                                InstantCast("Spear Hand Strike");
                            }
                        }
                        if (MIRsettings.Instance.LegSweep)
                        {
                            if (LegSweep() &&
                                !QuakingPalm() &&
                                !SpearHandStrike())
                            {
                                InstantCast("Leg Sweep");
                            }
                        }

                        #endregion

                        #region Brew

                        if (MIRsettings.Instance.EnergizingBrew)
                        {
                            if (EnergizingBrew() &&
                                !InvokeXuen() &&
                                !TouchofDeath())
                            {
                                InstantCast("Energizing Brew");
                            }
                        }
                        if (MIRsettings.Instance.FortifyingBrew)
                        {
                            if (FortifyingBrew() &&
                                !InvokeXuen() &&
                                !TouchofDeath())
                            {
                                InstantCast("Fortifying Brew");
                            }
                        }

                        #endregion

                        if (MIRsettings.Instance.SelectedValuecbx90 != null &&
                            MIRsettings.Instance.SelectedValuecbx90 == "Invoke Xuen, the White Tiger")
                        {
                            if (InvokeXuen() &&
                                !TouchofDeath())
                            {
                                InstantCast("Invoke Xuen, the White Tiger");
                            }
                        }
                        if (ChiBurst() &&
                            !TouchofDeath())
                        {
                            LongCast("Chi Burst");
                        }


                        if (MIRsettings.Instance.SpinningFireBlossom)
                        {
                            if (SpinningFireBlossom() &&
                                !DampenHarm() &&
                                !FistsofFury() &&
                                !TouchofDeath())
                            {
                                InstantCast("Spinning Fire Blossom");
                            }
                        }
                        if (MIRsettings.Instance.CracklingJadeLightning)
                        {
                            if (CracklingJadeLightning() &&
                                !TouchofKarma() &&
                                !TouchofDeath())
                            {
                                LongCast("Crackling Jade Lightning");
                            }
                        }
                        if (MIRsettings.Instance.TouchOfDeath)
                        {
                            if (TouchofDeath())
                            {
                                InstantCast("[Finisher Move]: Touch of Death");
                            }
                        }
                        if (MIRsettings.Instance.ZenMeditation)
                        {
                            if (ZenMeditation() &&
                                !TouchofDeath())
                            {
                                WoWMovement.MoveStop();
                                LongCast("Zen Meditation");
                                WoWMovement.MoveStop();
                            }
                        }
                    }
                }
        }

    /*if (StyxWoW.Me.HealthPercent < 70 && StyxWoW.Me.Inventory.Equipped.Trinket2.Cooldown == 0
                    && !StyxWoW.Me.IsCasting && !StyxWoW.Me.IsChanneling)
                {
                    Logging.Write("Using Trinket2 on Low Health");
                    StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                }*/

                /*if (CanCast("Diffuse Magic") &&
                    StyxWoW.Me.CurrentTarget.Class == WoWClass.Mage &&
                    StyxWoW.Me.CurrentTarget.IsCasting) // Counter the mage
                {
                    Logging.Write("Casting Diffuse Magic on Silenced");
                    InstantCast("Diffuse Magic");
                }*/
        


        public static List<WoWUnit> Adds
        {
            get
            {
                var ret = from o in ObjectManager.GetObjectsOfType<WoWUnit>(true, false)
                          where
                              o.IsValid &&
                              o.Distance < 15 &&
                              (StyxWoW.Me.CurrentTarget == null || o != StyxWoW.Me.CurrentTarget) &&
                              !o.IsFriendly &&
                              !o.IsTotem &&
                              !o.IsPet &&
                              (o.IsTargetingMeOrPet ||
                                  o.IsTargetingMyPartyMember ||
                                  o.IsTargetingMyRaidMember
                              )
                          orderby o.DistanceSqr ascending
                          select o;
                return ret.ToList();
            }
        }
    }
}
    
